package netty.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

import java.nio.charset.Charset;

/**
 * Created by IntelliJ
 * Author:熊磊
 */
public class ByteBuf2 {
    public static void main(String[] args) {

        //默认好像是UTF-8的编码
        ByteBuf byteBuf = Unpooled.copiedBuffer("hello,world!北京", Charset.defaultCharset());  // 底层也是数组 , writeIndex = 12 + 2*3 =18

        System.out.println(new String(byteBuf.array(), Charset.forName("UTF-8")));
        System.out.println(byteBuf.arrayOffset());
        System.out.println(byteBuf);
        System.out.println(byteBuf.readerIndex());
        System.out.println(byteBuf.capacity());
        System.out.println(byteBuf.readableBytes()); //可读取的字节数

        /**
         * 读取索引
         * 读取长度
         * 编码
         */
        System.out.println(byteBuf.getCharSequence(0,18,Charset.defaultCharset()));
    }
}
